﻿using BP.CCBill;
using BP.DA;
using BP.En;
using BP.Sys;
using BP.WF.Template.Frm;

namespace BP.Sys.Printer
{

    /// <summary>
    /// 打印模板属性
    /// </summary>
    public class PrintTemplateAttr : EntityMyPKAttr
    {
        /// <summary>
        /// 路径
        /// </summary>
        public const string Name = "Name";
        /// <summary>
        /// 路径
        /// </summary>
        public const string TempFilePath = "TempFilePath";
        /// <summary>
        /// NodeID
        /// </summary>
        public const string NodeID = "NodeID";
        /// <summary>
        /// 流程编号
        /// </summary>
        public const string FlowNo = "FlowNo";
        /// <summary>
        /// 字段名称
        /// </summary>
        public const string KeyOfEn = "KeyOfEn";
        /// <summary>
        /// 为生成单据使用
        /// </summary>
        public const string Idx = "Idx";
        /// <summary>
        /// 单据类型
        /// </summary>
        public const string TemplateFileModel = "TemplateFileModel";
        /// <summary>
        /// 是否生成PDF
        /// </summary>
        public const string PrintFileType = "PrintFileType";
        /// <summary>
        /// 二维码生成方式
        /// </summary>
        public const string QRModel = "QRModel";
        /// <summary>
        /// 文件打开方式
        /// </summary>
        public const string PrintOpenModel = "PrintOpenModel";
        /// <summary>
        /// 表单的ID
        /// </summary>
        public const string FrmID = "FrmID";

        /// <summary>
        /// VSTOword打印模板文件
        /// </summary>
        public const string VSTOWordPrintFile = "VSTOWordPrintFile";

        /// <summary>
        /// VSTOexcel打印模板文件
        /// </summary>
        public const string VSTOExcelPrintFile = "VSTOExcelPrintFile";

        /// <summary>
        /// 模板文件
        /// </summary>
        public const string DBFile = "DBFile";
    }
    /// <summary>
    /// 打印模板
    /// </summary>
    public class PrintTemplate : EntityMyPK
    {
        #region  属性
        /// <summary>
        /// UI界面上的访问控制
        /// </summary>
        public override UAC HisUAC
        {
            get
            {
                UAC uac = new UAC();
                uac.OpenForSysAdmin();
                return uac;
            }
        }
        /// <summary>
        /// 编号
        /// </summary>
        public new string MyPK
        {
            get
            {
                string no = GetValStrByKey("MyPK");
                no = no.Replace("\n", "");
                no = no.Replace(" ", "");
                return no;
            }
            set
            {
                SetValByKey("MyPK", value);
                SetValByKey(PrintTemplateAttr.TempFilePath, value);
            }
        }
        /// <summary>
        /// 生成的单据类型
        /// </summary>
        public PrintFileType HisPrintFileType
        {
            get
            {
                return (PrintFileType)GetValIntByKey(PrintTemplateAttr.PrintFileType);
            }
            set
            {
                SetValByKey(PrintTemplateAttr.PrintFileType, (int)value);
            }
        }
        /// <summary>
        /// 二维码生成方式
        /// </summary>
        public QRModel QRModel
        {
            get
            {
                return (QRModel)GetValIntByKey(PrintTemplateAttr.QRModel);
            }
            set
            {
                SetValByKey(PrintTemplateAttr.QRModel, (int)value);
            }
        }
        public TemplateFileModel TemplateFileModel
        {
            get
            {
                return (TemplateFileModel)GetValIntByKey(PrintTemplateAttr.TemplateFileModel);
            }
            set
            {
                SetValByKey(PrintTemplateAttr.TemplateFileModel, (int)value);
            }
        }

        /// <summary>
        /// 生成的单据打开方式
        /// </summary>
        public PrintOpenModel PrintOpenModel
        {
            get
            {
                return (PrintOpenModel)GetValIntByKey(PrintTemplateAttr.PrintOpenModel);
            }
            set
            {
                SetValByKey(PrintTemplateAttr.PrintOpenModel, (int)value);
            }
        }
        /// <summary>
        /// 打开的连接
        /// </summary>
        public string TempFilePath
        {
            get
            {
                string s = GetValStrByKey(PrintTemplateAttr.TempFilePath);
                if (DataType.IsNullOrEmpty(s) == true)
                    return MyPK;
                return s;
            }
            set
            {
                SetValByKey(PrintTemplateAttr.TempFilePath, value);
            }
        }
        /// <summary>
        /// 节点ID
        /// </summary>
        public int NodeID
        {
            get
            {
                return GetValIntByKey(PrintTemplateAttr.NodeID);
            }
            set
            {
                SetValByKey(PrintTemplateAttr.NodeID, value);
            }
        }
        public string Name
        {
            get
            {
                return GetValStringByKey(PrintTemplateAttr.Name);
            }
            set
            {
                SetValByKey(PrintTemplateAttr.Name, value);
            }
        }
        public string FrmID
        {
            get
            {
                return GetValStringByKey(PrintTemplateAttr.FrmID);
            }
            set
            {
                SetValByKey(PrintTemplateAttr.FrmID, value);
            }
        }
        #endregion

        #region 构造函数
        /// <summary>
        /// 打印模板
		/// </summary>
		public PrintTemplate() { }
        /// <summary>
        /// 打印模板
        /// </summary>
        /// <param name="mypk">主键</param>
        public PrintTemplate(string mypk) : base(mypk.Replace("\n", "").Trim())
        {
        }
        /// <summary>
        /// 获得单据文件流
        /// </summary>
        /// <param name="oid"></param>
        /// <returns></returns>
        public byte[] GenerFile()
        {
            byte[] bytes = DBAccess.GetByteFromDB("Sys_FrmPrintTemplate", "MyPK", this.MyPK, PrintTemplateAttr.DBFile);
            if (bytes != null)
                return bytes;

            //如果没有找到，就看看默认的文件是否有.
            string tempExcel = BP.Difference.SystemConfig.PathOfDataUser + "FrmVSTOTemplate/" + this.MyPK + ".xlsx";
            if (System.IO.File.Exists(tempExcel) == false)
                tempExcel = BP.Difference.SystemConfig.PathOfDataUser + "FrmVSTOTemplate/EmptyTemplate.xlsx";
            bytes = DataType.ConvertFileToByte(tempExcel);

            return bytes;
        }

        public byte[] GenerWordFile()
        {
            byte[] bytes = DBAccess.GetByteFromDB("Sys_FrmPrintTemplate", "MyPK", this.MyPK, PrintTemplateAttr.DBFile);
            if (bytes != null)
                return bytes;

            //如果没有找到，就看看默认的文件是否有.
            string tempWord = BP.Difference.SystemConfig.PathOfDataUser + "FrmVSTOTemplate/" + this.MyPK + ".docx";
            if (System.IO.File.Exists(tempWord) == false)
                tempWord = BP.Difference.SystemConfig.PathOfDataUser + "FrmVSTOTemplate/EmptyTemplate.docx";
            bytes = DataType.ConvertFileToByte(tempWord);

            return bytes;
        }
        /// <summary>
        /// 重写基类方法
        /// </summary>
        public override Map EnMap
        {
            get
            {
                if (_enMap != null)
                    return _enMap;
                Map map = new Map("Sys_FrmPrintTemplate", "打印模板数据源");
                map.IndexField = PrintTemplateAttr.FrmID;

                map.AddMyPK();
                map.AddTBString(PrintTemplateAttr.FrmID, null, "表单ID", false, false, 0, 60, 60);
                map.AddTBString(PrintTemplateAttr.Name, null, "模板名称", true, false, 0, 200, 100);
                map.AddTBString(PrintTemplateAttr.TemplateFileModel, null, "模板类型(rtf,excel,word,wps)", false, false, 0, 200, 200);

                string cfg1 = "@0=Word@1=PDF@2=Excel";
                map.AddDDLSysEnum(PrintTemplateAttr.PrintFileType, 0, "生成的文件类型", true, true, "PrintFileType", cfg1);

                //DBFile 存储二进制文件.

                // map.AddDDLSysEnum(PrintTemplateAttr.AthSaveWay, 0,"实例的保存方式", true, true, PrintTemplateAttr.AthSaveWay, cfg3);
                //map.AddDDLSysEnum(PrintTemplateAttr.QRModel, 0,"二维码生成方式", true, true, PrintTemplateAttr.QRModel,"@0=不生成@1=生成二维码');
                //  map.AddTBInt('Idx", 0,"Idx", false, false);
                // map.AddTBInt("Idx", 0, "Idx", false, false);
                _enMap = map;
                return _enMap;
            }
        }
        #endregion

        protected override bool beforeInsert()
        {
            if (DataType.IsNullOrEmpty(MyPK) == true)
                MyPK = DBAccess.GenerGUID();
            return base.beforeInsert();
        }
    }
    /// <summary>
    /// 打印模板s
    /// </summary>
    public class PrintTemplates : EntitiesMyPK
    {
        #region 构造
        /// <summary>
        /// 得到它的 Entity 
        /// </summary>
        public override Entity GetNewEntity
        {
            get
            {
                return new PrintTemplate();
            }
        }
        /// <summary>
        /// 打印模板
        /// </summary>
        public PrintTemplates()
        {
        }
        public PrintTemplates(int nodeID)
        {
            Retrieve(PrintTemplateAttr.NodeID, nodeID);
        }
        public PrintTemplates(string flowNo)
        {
            Retrieve(PrintTemplateAttr.FlowNo, flowNo);
        }
        #endregion

        #region 为了适应自动翻译成java的需要,把实体转换成List.
        /// <summary>
        /// 转化成 java list,C#不能调用.
        /// </summary>
        /// <returns>List</returns>
        public System.Collections.Generic.IList<PrintTemplate> ToJavaList()
        {
            return (System.Collections.Generic.IList<PrintTemplate>)this;
        }
        /// <summary>
        /// 转化成list
        /// </summary>
        /// <returns>List</returns>
        public System.Collections.Generic.List<PrintTemplate> Tolist()
        {
            System.Collections.Generic.List<PrintTemplate> list = new System.Collections.Generic.List<PrintTemplate>();
            for (int i = 0; i < Count; i++)
            {
                list.Add((PrintTemplate)this[i]);
            }
            return list;
        }
        #endregion 为了适应自动翻译成java的需要,把实体转换成List.
    }

}
